// logic.cpp : the logic implementation

#include "logic.h"

Session * mainSession;

//-------------Session-----------------
/*Function: Session constructor
  * begins game, 
  * add players, 
  * creates resource and building types*/
Session :: Session ()
{
    turnCounter = 0;
    printf ( "Created session\n");
    Player p1 ( "first Player", 0);
    players.push_back ( p1);
    Player p2 ( "second Player", 1);
    players.push_back ( p2);
    currentPlayerId = 0;
    createResources();
    createBTypes();

}

/*Function: turn
  Semantics: function for evaluate turn queue and all 
  params for every player*/
void Session :: turn()
{
    unsigned int i = currentPlayerId;

    printf ( "end of %d turn of %s (%d) with %d $\n", turnCounter, players [i].getName(), 
            players [i].getRank(), players [i].getMoney());
    //turn sequence
        //income
    players[i].evaluateIncome();
        //outcome
    players[i].evaluateMoney();
        //buying and building

        //is win?    
    currentPlayerId++;
    if (currentPlayerId == players.size()){ //last
        printf("turn %d ends; next turn begins\n", turnCounter);
        turnCounter++;
        currentPlayerId = 0;
    }    
}
int Session::totalOutcome(Player * player)
{
	int result = 0;
	for( unsigned int j = 0; j < player->getBuilding().size(); ++j){
		for( unsigned int i = 0; i < player->getBuilding()[j].getType()->consAmount.size(); i++){
			result += player->getBuilding()[j].getType()->cons[i].getPrice() * player->getBuilding()[j].getType()->consAmount[i];
		}
	}
	return result;
}

//-------------Player--------------------
/*Function: Player
  constructs default player*/
Player :: Player ()
{
    char *defName = "Default Player";
    name = new char [strlen ( defName) + 1];
    strcpy ( name, defName);

    money = defaultMoney;
    //rank =  mainSession->getPlayers().size();
    rank = mainSession->players.size();
    mainSession->players.push_back ( *this);

    //mainSession->getPlayers().push_back ( this);
    printf("created player: %s, %d\n", name, rank);
}

/*Function: Player
  constructs player with exact parameters*/
Player :: Player ( char * passedName, int passedRank)
{
    name = new char [strlen ( passedName) + 1];
    strcpy ( name, passedName);

    money = defaultMoney;
    rank =  passedRank;
    income = 0;

    printf("created player: %s, %d\n", name, rank);
}


Player :: ~Player ()
{
}

void Player :: addBuilding (Building *b)
{
    printf(" add building %s, %d, %d to player %s\n", 
        b->getName(), b->getPrice(), b->getIncome(), getName());
    printf(" set rank %d to %d\n", b->getOwner(), rank);
    b->setOwner( rank);
     
    buildings.push_back(*b);
}

void Player :: evaluateIncome ()
{
    int res = 0;
    for (unsigned int i = 0; i < buildings.size(); i++)
        res += buildings [i].getIncome();
    income = res;
}

//-------------Building------------------
/*Function: Building constructor*/
Building :: Building ( char * s, int pr, int inc = DefaultIncome) 
{
    name = new char (strlen(s)+2);
    strcpy (name, s);
    //strcmp ( name, n);
    price = pr;
    income = inc;
    owner = NoPlayer;
    //default type
    //type = mainSession->defaultType;
}

/*Function: Building constructor*/
Building :: Building () 
{
    name = new char (10);
    strcpy (name, "dname");
    price = 0;
    income = DefaultIncome;
    owner = NoPlayer;
    //consumptionRes.clear();
    //productionRes.clear();
    setType ( mainSession->defaultType);
    //printf ( "building : %s, %d \n", name, price);
};

/*Function: Building constructor*/
Building :: Building ( BType * type) 
{
    name = new char (10);
    strcpy (name, "dname");
    price = 0;
    income = DefaultIncome;
    owner = NoPlayer;
    //consumptionRes.clear();
    //productionRes.clear();
    setType ( type);
};


void Building :: setName ( char * n) 
{ 
    name = new char [strlen(n)+1]; 
    strcpy ( name, n);
};
void Building :: setIncome ()
{
    income = 0;
    income -= productionPrice;
    for( unsigned int i = 0; i < type->consAmount.size(); i++)
			income -= type->cons[i].getPrice() * type->consAmount[i];
    for( unsigned int i = 0; i < type->prodAmount.size(); i++)
			income += type->prod [i].getPrice() * type->prodAmount[i];
	
}


Resource :: Resource()
{
	price = defaultPrice;
	name = new char[strlen("res")+1];
	strcpy( name, "res");
}
Resource :: Resource( char * str, int pr)
{
	name = new char[strlen(str)+1];
	strcpy( name, str);
	price = pr;
}
void Resource::setPrice( int n)
{
	price = n;
}
int Resource::getPrice()
{
	return price;
}